local dx_json = require("dx_json")
local log = require("dx_logger")
local dx_sqlite3 = require("dx_sqlite3")
local t_permission = {}

-- 新增白名单
function t_permission.dx_sqlite_save_whitelists(data)
    data.extra = dx_json.encode(data.extra)
    data.time = dx_json.encode(data.time)
    dx_sqlite3.execute(
        sqlite_client,
        "INSERT INTO whitelists values('" ..
            data.id .. "'," .. data.type .. ",'" .. data.code .. "','" .. data.extra .. "','" .. data.time .. "')"
    )
    return true
end

-- 批量添加白名单
function t_permission.dx_sqlite_batch_save_whitelists(data)
    local ids = {}
    local batch_sql = "INSERT INTO whitelists ('id','type','code','extra','time') VALUES "
    for i, v in ipairs(data) do
        table.insert(ids, v.id)
        batch_sql =
            batch_sql ..
            "('" ..
                v.id ..
                    "'," ..
                        v.type ..
                            ",'" ..
                                v.code .. "','" .. dx_json.encode(v.extra) .. "','" .. dx_json.encode(v.time) .. "')"
        if #data ~= i then
            batch_sql = batch_sql .. ","
        end
    end
    dx_sqlite3.execute(sqlite_client, batch_sql)
    return true, ids
end

-- 批量删除白名单，根据id来删
function t_permission.dx_sqlite_del_whitelists(param)
    local area = ""
    for i, v in ipairs(param) do
        area = area .. "'" .. v .. "'"
        if #param ~= i then
            area = area .. ","
        end
    end
    dx_sqlite3.execute(sqlite_client, "delete from whitelists where id in (" .. area .. ")")
    return true
end

-- 清空白名单
function t_permission.dx_sqlite_clear_whitelists()
    dx_sqlite3.execute(sqlite_client, [[delete from whitelists]])
    return true
end

-- 修改白名单，根据id来查
function t_permission.dx_sqlite_update_whitelists(param)
    param.extra = dx_json.encode(param.extra)
    param.time = dx_json.encode(param.time)
    dx_sqlite3.execute(
        sqlite_client,
        "UPDATE whitelists SET type = " ..
            param.type ..
                ",code = '" ..
                    param.code ..
                        "',extra='" .. param.extra .. "',time='" .. param.time .. "' WHERE id = '" .. param.id .. "'"
    )
    return true
end

-- 分页查询白名单
function t_permission.dx_sqlite_select_whitelists(param)
    local rs_data = {}
    local sql = "SELECT * FROM whitelists "
    local condition = {}
    if param.id then
        table.insert(condition, " id like '%" .. param.id .. "%' ")
    end
    if param.type then
        table.insert(condition, " type like '%" .. param.type .. "%' ")
    end
    if param.code then
        table.insert(condition, " code like '%" .. param.code .. "%' ")
    end
    if param.extra then
        table.insert(condition, " extra like '%" .. param.extra .. "%' ")
    end
    if param.time then
        table.insert(condition, " time like '%" .. param.time .. "%' ")
    end

    if #condition ~= 0 then
        sql = sql .. " where "
        for i, v in ipairs(condition) do
            sql = sql .. v
            if #condition ~= i then
                sql = sql .. " and "
            end
        end
    end

    local begin_index = (param.page - 1) * param.size
    sql = sql .. " LIMIT " .. begin_index .. "," .. param.size
    local result = dx_sqlite3.query(sqlite_client, sql)
    for key, value in pairs(result) do
        -- 避免指针导致查出来的数据全部相同的问题
        local temp = dx_json.encode(value)
        temp = dx_json.decode(temp)
        table.insert(rs_data, temp)
    end
    return true, rs_data
end

-- 查询白名单总数
function t_permission.dx_sqlite_total_count_whitelists()
    local rs_data = 0
    local result = dx_sqlite3.query(sqlite_client, "select count(*) as count from whitelists")
    for key, value in pairs(result) do
        rs_data = value.count
        break
    end
    return true, rs_data
end

-- 查询白名单中所有的数据
function t_permission.sqlite_query_all_white_list()
    local whitelist_list = {}
    local sql = "SELECT * FROM whitelists"
    local result = dx_sqlite3.query(sqlite_client, sql)
    if result and #result ~= 0 then
        for key, value in pairs(result) do
            local per_rs = {}
            per_rs.type = value.type
            per_rs.code = value.code
            per_rs.id = value.id
            per_rs.extra = value.extra
            per_rs.time = value.time
            table.insert(whitelist_list, per_rs)
        end
    end
    return whitelist_list
end

-- 根据IdentityType和IdentityCode查询白名单
function t_permission.queryWhiteListByCode(code, type)
    local IdentityInfo = nil
    local sql = "select * from whitelists where type =  " .. type .. " and code ='" .. code .. "' limit 1"
    local result = dx_sqlite3.query(sqlite_client, sql)
    for key, value in pairs(result) do
        IdentityInfo = {}
        return value
    end
    return IdentityInfo
end

return t_permission
